package com.helei.data.config.mq;


import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.Map;

@Component
@RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
public class DirectReceiver {
  /*  @Resource
    OperatelogMapper operatelogMapper;
*/
   /* @RabbitHandler
    public void process(byte[] bytes) {
        //Base64 Encoded
        String s = new String(bytes);
        System.out.println("DirectReceiver消费者收到消息  : " + s);
    }*/

/*    @RabbitHandler
    public void process(String message, Channel channel, @Headers Map<String, Object> map) {
        System.out.println("DirectReceiver消费者收到消息  : " + message);
        try {
            Thread.sleep(10000);
         *//*   OperateLog operateLog = new Gson().fromJson(message, OperateLog.class);
            operatelogMapper.insert(operateLog);*//*
            Integer a = Integer.valueOf(message);
            channel.basicAck((Long) map.get(AmqpHeaders.DELIVERY_TAG), false);//确认消息
            System.out.println("确认消息------------");
            LocalDateTime end = LocalDateTime.now();
            DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
            System.out.println("消息消费完成时间-----------------------" + dtf2.format(end));
        } catch (Exception e) {
            System.out.println("否认消息------------");
            try {
               // channel.basicNack((Long)map.get(AmqpHeaders.DELIVERY_TAG),false,true);      //否认消息

                channel.basicReject((Long)map.get(AmqpHeaders.DELIVERY_TAG),false);        //拒绝消息

            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            e.printStackTrace();

        }*/

    @RabbitHandler
    public void process(byte[] bytes, Channel channel, @Headers Map<String, Object> map) {
        String s = new String(bytes);
        System.out.println("DirectReceiver消费者收到消息  : " + s);
        try {
            Thread.sleep(5000);
            channel.basicAck((Long) map.get(AmqpHeaders.DELIVERY_TAG), false);//确认消息
            System.out.println("确认消息------------");
            LocalDateTime end = LocalDateTime.now();
            DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
            System.out.println("消息消费完成时间-----------------------" + dtf2.format(end));
        } catch (Exception e) {
            System.out.println("否认消息------------");
            try {
                // channel.basicNack((Long)map.get(AmqpHeaders.DELIVERY_TAG),false,true);      //否认消息

                channel.basicReject((Long) map.get(AmqpHeaders.DELIVERY_TAG), false);        //拒绝消息

            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            e.printStackTrace();

        }
    }
}
